// pages/car_search/car_search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //滚动区域高度
    height: '',
    scroll_h:'',
    carList: [],
    allCar:[],
    car_gold:'50',
  },
  //选择框改变事件
  checkChange (e) {
    let checkList = e.detail.value;
    let allCar = this.data.allCar
    let carList = []
    for (let item of checkList) {
      allCar.forEach(car => {
        if(car.car_type == item){
          carList.push(car)
        }
      });
    }
    this.setData({
      carList:carList
    })

  },
  // 车辆点击事件
  carsTap (e) {
    let index = e.currentTarget.dataset.index
    let car_id = e.currentTarget.dataset.id
    let person_id = wx.getStorageSync('user').user_id
    let user_id = this.data.carList[index].user_id
    let car_gold = this.data.carList[index].car_gold
    let that = this
    wx.request({
      url: 'http://121.37.209.61:8888/user/rentCar', 
      method:'post',
      data: {
        car_id:car_id,
        person_id:person_id,
        user_id:user_id,
        car_gold:car_gold
      },
      success (res) {
        if(res.data.code == 200){
          wx.showToast({
            title: '租用成功',
            icon: 'success',
            duration: 1500
          })
          that.data.carList[index].car_statu = '0'
          that.setData({
            carList:that.data.carList
          })
        }else{
          wx.showToast({
            title: '失败请重试!',
            icon: 'error',
            duration: 1500
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    // 监听窗口高度
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          height: res.windowHeight,
          scroll_h:res.windowHeight-75
        })
      },
    })
    //请求数据
    let that = this
    wx.request({
      url: 'http://121.37.209.61:8888/car/all',
      method:'get',
      data:{

      },
      success(res){
        that.setData({
          allCar:res.data.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {

  }
})